const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
module.exports = {
  // 返回正确
  resSuccess(data) {
    const { ctx } = this;
    ctx.status = 200;
    ctx.body = {
      message: "SUCCESS",
      data:data
    };
  },
  // 返回错误
  resError(message) {
    const { ctx } = this;
    ctx.status = 400;
    ctx.body = {
      message: message
    };
  },
  // 发送邮箱
  pushEmail(config) {
    return new Promise((reslove,reject) => {
      const transport = nodemailer.createTransport(smtpTransport({
        host: 'smtp.qq.com',
        port: 465,
        secure: true,
        auth: {
          user: '540994598@qq.com',
          pass: 'pzjbjhaqmcytbbjg'
        }
      }));
      transport.sendMail({
        from: '540994598@qq.com',
        to: config.email,
        subject: config.subject,
        html: config.html
      }, (error, data) => {
          if(error){
              transport.close();
              reject(error);
          }else{
              transport.close();
              reslove(data);
          }
      })
    });
  }
};
